<?php
class model_group extends database_mysql
{
    public $_table = 'groups';
    public function mdg_get_list_data($table, $page=1,$limit=50)
    {
        $str = null;
        $table = prefix . $table;
        if(!empty($limit))
        {
			$page=($page==0 || empty($page)?1:$page);
			$start = ($page - 1)* $limit; 
			$strlimit = "LIMIT $start, $limit";
		}
		else{
			$strlimit ='';
		}		 
        
        $sql = "SELECT *
                FROM
                ". $table ."
                ORDER BY ". $table .".ID DESC ".$strlimit;
        $row = parent::mysql_fetch_all_rows($sql);
        parent::mysql_close_connection();
 		return $row;
    }
    
    public function mdg_get_total($table)
    {
        $sql = "SELECT COUNT( * ) AS total FROM ". prefix .$table;
        $result = parent::mysql_select_one($sql);
        return  $result;
    }
    
	public function mdg_get_list_seo_key()
    {
        $row = parent::mysql_fetch_all_rows(" 
                    SELECT
                    ". prefix ."categories_lang.SeoKey,
                    ". prefix ."categories.ID
                    FROM
                    ". prefix ."categories
                    INNER JOIN ". prefix ."categories_lang ON ". prefix ."categories.ID = ". prefix ."categories_lang.CatID
                    WHERE
                    ". prefix ."categories_lang.`Status` = 'show'");
 		return $row;  
    }
    
    public function mdg_get_action_from_seokey($seo_key){
        $sql = " 
                    SELECT
                    ". prefix ."actions.ActName
                    FROM
                    ". prefix ."categories_lang
                    INNER JOIN ". prefix ."categories ON ". prefix ."categories_lang.CatID = ". prefix ."categories.ID
                    INNER JOIN ". prefix ."com_act ON ". prefix ."categories.Com = ". prefix ."com_act.ComID
                    INNER JOIN ". prefix ."actions ON ". prefix ."com_act.ActID = ". prefix ."actions.ID
                    WHERE
                    ". prefix ."categories_lang.CatID = '".$seo_key."' AND
                    ". prefix ."categories_lang.`Status` = 'show' AND
                    ". prefix ."com_act.`Status` = 'true' AND
                    ". prefix ."actions.`Status` = 'true'
        ";
        $row = parent::mysql_fetch_all_rows($sql);
 		return $row; 
    }
    
    public function mdg_insert_data($table, $data){
        $table = prefix . $table;
        parent::mysql_insert_array($table, $data);
    }
    
    public function mdg_update_data($table, $data, $condition){
        $table = prefix . $table;
        parent::mysql_update_array($table, $data, $condition);
    }

    public function mdg_tool_func($table, $model,$active,$str_id)
    {
        $done = false;
        switch($active)
        {
            case 'show':
            case 'hide':
                $status = array('show'=>'true', 'hide'=>'false');
                $done = $model->model_tool_set_status(prefix.$table,$status[$active]," ID IN(".trim($str_id).")");
                
            break;

            case 'delete':
                $done = $model->model_tool_delete(prefix.$table," ID IN(".trim($str_id).")" );
                $done = $model->model_tool_delete(prefix.'group_permiss'," GroupID IN(".trim($str_id).")" );
            break;
        }

        return $done;
    }
    
    public function mdg_get_last_id($table)
    {
    	$result = parent::mysql_selected("SELECT ID FROM ". prefix ."$table ORDER BY id DESC LIMIT 1");
    	$result = mysql_fetch_array($result);
    	return $result['ID'];
    }
    
    public function mdg_get_group_permission($id)
    {
        $sql = "
                SELECT
                ". prefix ."group_permiss.PremissID,
                ". prefix ."group_permiss.GroupID
                FROM
                ". prefix ."group_permiss
                WHERE
                ". prefix ."group_permiss.GroupID = 
        ".$id;
        $row = parent::mysql_fetch_all_rows($sql);
 		return $row;
    }
    
    public function mdg_get_group($id){
        $sql = "
                SELECT *
                FROM
                ". prefix ."groups
                WHERE
                ". prefix ."groups.ID =
        ".$id;
        $row = parent::mysql_fetch_one_rows($sql);
 		return $row;
    }
    
    public function mdg_del_item($table, $field, $condition)
    {
        parent::mysql_delete("DELETE FROM `". prefix ."$table` WHERE `". prefix ."$table`.`$field` IN($condition)");
        
    }
}

